package servlet;

import biz.EmpBiz;
import biz.impl.EmpBizImpl;
import com.sun.org.apache.xerces.internal.impl.xs.XSElementDecl;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.example.bean.EmpInfo;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/emp4")
public class EmpServlet extends HttpServlet {
    private EmpBiz eb;
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String type = request.getParameter("type");
        if (null==type || "find".equals(type)){
            find(request,response);
        }else if ("search".equals(type)){
            search(request,response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
    protected void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        ServletContext application = session.getServletContext();
        SqlSessionFactory ssf = (SqlSessionFactory)application.getAttribute("SQL_SESSION_FACTORY");
        SqlSession s = ssf.openSession();
        eb = new EmpBizImpl(s);

        session.setAttribute("ALL_EMP_LIST",eb.findAllEmp());
        session.setAttribute("ALL_JOB",eb.findAllEmpJob());
        session.setAttribute("ALL_MGR",eb.findAllEmpMgr());
        s.close();

        response.sendRedirect("index.jsp");
    }
    protected void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        ServletContext application = session.getServletContext();
        SqlSessionFactory ssf = (SqlSessionFactory) application.getAttribute("SQL_SESSION_FACTORY");
        SqlSession s = ssf.openSession();
        eb = new EmpBizImpl(s);

        EmpInfo ei = new EmpInfo();
        String name = request.getParameter("ename");
        String job = request.getParameter("job");
        int mgr = Integer.parseInt(request.getParameter("mgr"));
        String ms1 = request.getParameter("minSal");
        String ms2 = request.getParameter("maxSal");
        String beginDate = request.getParameter("beginDate");
        String endDate = request.getParameter("endDate");
        String[] jobs = request.getParameterValues("jobs");
        if (!"".equals(ms1)){
            double minSal = Double.parseDouble(ms1);
            ei.setMinSal(minSal);
        }
        if (!"".equals(ms2)){
            double maxSal = Double.parseDouble(ms2);
            ei.setMaxSal(maxSal);
        }
        if (null==jobs||jobs.length==0){
            ei.setJobs(null);
        }else {
            ei.setJobs(jobs);
        }
        ei.setEname(name);
        ei.setJob(job);
        ei.setMgr(mgr);
        ei.setBeginDate(beginDate);
        ei.setEndDate(endDate);
        session.setAttribute("ALL_EMP_LIST",eb.findEmp(ei));
        s.close();
        response.sendRedirect("index.jsp");
    }
}
